package com.myxfd.superweibo01;

import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.myxfd.superweibo01.api.SuperService;
import com.myxfd.superweibo01.api.TngouService;
import com.myxfd.superweibo01.model.Gallery;
import com.myxfd.superweibo01.model.GalleryClass;
import com.myxfd.superweibo01.model.GalleryClassList;
import com.myxfd.superweibo01.model.GalleryList;
import com.myxfd.superweibo01.model.ServerInfo;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MainActivity extends AppCompatActivity {
    private SuperService mSuperService;
    private TngouService mTngouService;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (mTngouService == null) {
            // step1. 简单案例
            Retrofit.Builder builder = new Retrofit.Builder();
            builder.baseUrl("http://www.tngou.net/");
            // TODO: 2016/10/26 检测字符串是否可用
            //转换器按照添加的顺序, 优先处理, 建议先添加基本类型转换

            // 支持服务器返回的数据转换为字符串
            builder.addConverterFactory(ScalarsConverterFactory.create());
            // 添加 GSON 转换对象的支持
            builder.addConverterFactory(GsonConverterFactory.create());


            Retrofit retrofit = builder.build();
            //创建TngouService的实现类, 用于发起实际的网络请求
            mTngouService = retrofit.create(TngouService.class);
        }

        if (mSuperService == null) {
            Retrofit.Builder builder = new Retrofit.Builder();
            builder.baseUrl("http://10.0.153.115:8080/rest/api/");
            builder.addConverterFactory(ScalarsConverterFactory.create());
            builder.addConverterFactory(GsonConverterFactory.create());
            Retrofit retrofit = builder.build();
            mSuperService = retrofit.create(SuperService.class);
        }


    }

    public void btnGetClassify(View view) {

        // step2: 调用 Service 的方法
        //方法返回 Call 对象, 用于实际的网络请求, 并且返回结果
        Call<GalleryClassList> call = mTngouService.getGalleryClassify();

        //同步代码
        //call.execute();
        //异步代码
        call.enqueue(new Callback<GalleryClassList>() {
            @Override
            public void onResponse(Call<GalleryClassList> call, Response<GalleryClassList> response) {
                //请求成功, 返回数据, 注意!!!! 不代表状态码为200, 也包含 405, 400等其他状态码
                if (response.isSuccessful()) {
                    GalleryClassList body = response.body();
                    List<GalleryClass> galleryClasses = body.getClasses();
                    for (GalleryClass aClass : galleryClasses) {
                        String title = aClass.getTitle();
                        Log.d(TAG, "onResponse: title => " + title);
                    }
                }
            }

            @Override
            public void onFailure(Call<GalleryClassList> call, Throwable t) {
            }
        });
    }

    /**
     * 获取图片列表
     *
     * @param view
     */
    public void btnGetList(View view) {
        // 1. 服务对象
        Call<GalleryList> listCall = mTngouService.getListById("1");
        listCall.enqueue(new Callback<GalleryList>() {
            @Override
            public void onResponse(Call<GalleryList> call, Response<GalleryList> response) {
                if (response.isSuccessful()) {
                    GalleryList body = response.body();
                    List<Gallery> galleries = body.getGalleries();
                    for (Gallery gallery : galleries) {
                        String title = gallery.getTitle();
                        Log.d(TAG, "onResponse: title => " + title);
                    }
                }
            }

            @Override
            public void onFailure(Call<GalleryList> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    public void btnGetInfoVersion(View view) {
        Call<String> call = mSuperService.getInfoItem("version");
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    String body = response.body();
                    Log.d(TAG, "onResponse: body => " + body);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    public void btnPostLogin(View view) {
        //POST提交方式
        Call<String> call = mSuperService.login("zhanglulu", "123456");
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    String body = response.body();
                    Log.d(TAG, "onResponse: body =>" + body);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }

    public void btnPUTJSON(View view) {
        ServerInfo info = new ServerInfo();
        info.setAuthor("lulu");
        info.setName("超级超级服务器");
        info.setUpdateTime(String.valueOf(SystemClock.currentThreadTimeMillis()));
        info.setVersion("1.1");
        Call<String> call = mSuperService.updateInfo(info);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    String body = response.body();
                    Log.d(TAG, "onResponse: body => " + body);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });


    }
}
